
goog.provide('esn.model.FriendSurveyModel');

goog.require('esn.model.StudentModel');
goog.require('goog.object');


/**
 * @param {Object|esn.model.StudentModel} model A JSON model of a friend
 *     survey or a student model of the friend in the survey.
 * @constructor
 */
esn.model.FriendSurveyModel = function(model) {
  // If this is a number then we assume this is a new survey model and that
  // this number is the friend id.
  if (model instanceof esn.model.StudentModel) {
    model = {'friend': model};
  } else if ('friend' in model) {
    model['friend'] = new esn.model.StudentModel(model['friend']);
  }

  this.model_ = model;

  // Verify that the student_id and friend are set.
  if (!this.getFriend()) {
    throw new Error('The "friend" field must be set in the model.');
  }
};

esn.model.FriendSurveyModel.RelationType = {
  FLOOR: 0,
  DORM: 1,
  CLASS: 2,
  HOBBIES: 3,
  OTHER: 4,
  UNKNOWN: 5
};

esn.model.FriendSurveyModel.prototype.getId = function() {
  return this.model_['id'];
};

esn.model.FriendSurveyModel.prototype.getStudentId = function() {
  return this.model_['student_id'];
};

esn.model.FriendSurveyModel.prototype.getFriend = function() {
  return this.model_['friend'];
};

/**
 * @return {esn.model.FriendSurveyModel.RelationType} The type of relationship.
 */
esn.model.FriendSurveyModel.prototype.getRelationType = function() {
  return this.model_['relation_type'];
};

/**
 * @param {esn.model.FriendSurveyModel.RelationType} relationType The type of
 *     relationship.
 */
esn.model.FriendSurveyModel.prototype.setRelationType = function(relationType) {
  this.model_['relation_type'] = relationType;
};

esn.model.FriendSurveyModel.prototype.getFriendshipLevel = function() {
  return this.model_['friendship_level'];
};

esn.model.FriendSurveyModel.prototype.setFriendshipLevel = function(
    friendshipLevel) {
  this.model_['friendship_level'] = friendshipLevel;
};

esn.model.FriendSurveyModel.prototype.getInteractionLevel = function() {
  return this.model_['interaction_level'];
};

esn.model.FriendSurveyModel.prototype.setInteractionLevel = function(
    interactionLevel) {
  this.model_['interaction_level'] = interactionLevel;
};

esn.model.FriendSurveyModel.prototype.getAskAdviceLevel = function() {
  return this.model_['ask_advice_level'];
};

esn.model.FriendSurveyModel.prototype.setAskAdviceLevel = function(
    askAdviceLevel) {
  this.model_['ask_advice_level'] = askAdviceLevel;
};

esn.model.FriendSurveyModel.prototype.getTrustLevel = function() {
  return this.model_['trust_level'];
};

esn.model.FriendSurveyModel.prototype.setTrustLevel = function(trustLevel) {
  this.model_['trust_level'] = trustLevel;
};

esn.model.FriendSurveyModel.prototype.isRandomFriend = function() {
  return goog.isDef(this.model_['is_random_friend']) ?
      this.model_['is_random_friend'] : false;
};

esn.model.FriendSurveyModel.prototype.setIsRandomFriend = function(
    isRandomFriend) {
  this.model_['is_random_friend'] = isRandomFriend;
};

// This is only valid for a randomly selected friend.
esn.model.FriendSurveyModel.prototype.getFirstMetLocation = function() {
  this.validateRandomFriend_();
  return this.model_['first_met_location'];
};

// This is only valid for a randomly selected friend.
esn.model.FriendSurveyModel.prototype.setFirstMetLocation = function(
    firstMetLocation) {
  this.validateRandomFriend_();
  this.model_['first_met_location'] = firstMetLocation;
};

// This is only valid for a randomly selected friend.
esn.model.FriendSurveyModel.prototype.getFirstMetInteraction = function() {
  this.validateRandomFriend_();
  return this.model_['first_met_interation'];
};

// This is only valid for a randomly selected friend.
esn.model.FriendSurveyModel.prototype.setFirstMetInteraction = function(
    firstMetInteraction) {
  this.validateRandomFriend_();
  this.model_['first_met_interation'] = firstMetInteraction;
};

esn.model.FriendSurveyModel.prototype.validateRandomFriend_ = function() {
  if (!this.isRandomFriend()) {
    throw new Error('This friend is not marked as a randomly selected friend.');
  }
};

esn.model.FriendSurveyModel.prototype.toObject = function() {
  var model = goog.object.clone(this.model_);
  model['friend'] = this.getFriend().toObject();
  return model;
};

esn.model.FriendSurveyModel.prototype.mergeFrom = function(model) {
  // Set any fields present in the new model that are not in the current model.
  // Note: No existing values are overridden.
  goog.object.forEach(model.toObject(), function(value, key) {
    if (!(key in this.model_)) {
      if (key == 'friend') {
        this.model_[key] = new esn.model.StudentModel(value);
      } else {
        this.model_[key] = value;
      }
    }
  }, this);
};

esn.model.FriendSurveyModel.fullNameComparator = function(model1, model2) {
  var name1 = model1.getFriend().getFullName().toLowerCase();
  var name2 = model2.getFriend().getFullName().toLowerCase();
  if (name1 < name2) {return -1}
  if (name1 > name2) {return 1}
  return 0;
};
